package com.dream.inspect.modules.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.Query;
import com.dream.inspect.modules.business.dao.SuperviseProjectRecordDao;
import com.dream.inspect.modules.business.entity.SuperviseProjectRecordEntity;
import com.dream.inspect.modules.business.service.SuperviseProjectRecordService;


@Service("superviseProjectRecordService")
public class SuperviseProjectRecordServiceImpl extends ServiceImpl<SuperviseProjectRecordDao, SuperviseProjectRecordEntity> implements SuperviseProjectRecordService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SuperviseProjectRecordEntity> page = this.selectPage(
                new Query<SuperviseProjectRecordEntity>(params).getPage(),
                new EntityWrapper<SuperviseProjectRecordEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public List<SuperviseProjectRecordEntity> findSuperviseProjectRecordList(Map<String, String> params) {
		int currPage = Integer.valueOf(params.get("currPage"));
    	int pageSize  = Integer.valueOf(params.get("pageSize"));
    	currPage = (currPage - 1) * pageSize;
		List<SuperviseProjectRecordEntity> sprList = new ArrayList<SuperviseProjectRecordEntity>();
		//组装条件
		EntityWrapper<SuperviseProjectRecordEntity> wrapper = addCondition(params);
		sprList = baseMapper.findSuperviseProjectRecordList(wrapper, currPage, pageSize);
		return sprList;
	}

	@Override
	public int findSuperviseProjectRecordListCount(Map<String, String> params) {
		//组装条件
		EntityWrapper<SuperviseProjectRecordEntity> wrapper = addCondition(params);
		int countNum = baseMapper.findSuperviseProjectRecordListCount(wrapper);
		return countNum;
	}
	
	@Override
	public Long insertSuperviseProjectRecordService(Map<String, String> params, Long spId) {
		
		Date now = new Date();
        String userId = params.get("userId");
        String userName = params.get("userName");
        String userDeptId = params.get("userDeptId");
        String czzt = params.get("czzt");
        String bhlxyy  = params.get("bhlxyy");
        String bhbjyy = params.get("bhbjyy");

		SuperviseProjectRecordEntity sprEntity = new SuperviseProjectRecordEntity();
		sprEntity.setProjectId(spId);                            //督察专项ID
		                                                         //操作状态(1：申请立项，2：取消立项，3：通过立项，4：驳回立项，5：执行立项，6：申请办结，7：取消办结，8：通过办结，9：驳回办结)
		if (StringUtils.isNotEmpty(userName)) {                  //操作人员名称
			sprEntity.setCzryName(userName);
		}
		if (StringUtils.isNotEmpty(userDeptId)) {                //操作人单位ID
			sprEntity.setCzdwId(Long.valueOf(userDeptId));
		}
		if (StringUtils.isNotEmpty(czzt)) {                      //操作状态(1：申请立项，2：取消立项，3：通过立项，4：驳回立项，5：执行立项，6：申请办结，7：取消办结，8：通过办结，9：驳回办结)
			sprEntity.setCzzt(czzt);
		}
		if (StringUtils.isNotEmpty(bhlxyy)) {                      //驳回立项原因
			sprEntity.setBhlxyy(bhlxyy);
		}
		if (StringUtils.isNotEmpty(bhbjyy)) {                      //驳回办结原因
			sprEntity.setBhbjyy(bhbjyy);
		}
		
		sprEntity.setCreateUser(Long.valueOf(userId));           //创建人id
		sprEntity.setCreateTime(now);                            //创建时间
		sprEntity.setUpdateUser(Long.valueOf(userId));           //更新人id
		sprEntity.setUpdateTime(now);   
		
		//save
		this.insert(sprEntity);
		
		return sprEntity.getId();
	}

	
	/**
	 * 拼接查询条件
	 * 
	 * @param params
	 * @return
	 */
	private EntityWrapper<SuperviseProjectRecordEntity> addCondition(Map<String, String> params) {
		String id = params.get("id");
		String projectId = params.get("projectId");
		
		EntityWrapper<SuperviseProjectRecordEntity> wrapper = new EntityWrapper<SuperviseProjectRecordEntity>();
		wrapper.setEntity(new SuperviseProjectRecordEntity());
		//督察记录ID
		if (StringUtils.isNotEmpty(id)) {
			wrapper.eq("ID", id);
		}
		//专项ID
		if (StringUtils.isNotEmpty(projectId)) {
			wrapper.eq("PROJECT_ID", projectId);
		}
    
    	return wrapper;
	}

}
